package cn.itcast.web.controller.cargo;

import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.ContractService;
import cn.itcast.service.cargo.ExportProductService;
import cn.itcast.service.cargo.ExportService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/cargo/export")
public class ExportController extends BaseController {

 @Reference
 private ContractService contractService;

 @Reference
 private ExportService exportService;

 @Reference
 private ExportProductService exportProductService;

 /**
  * 合同管理：查询所有的购销合同
  * /cargo/export/contractList.do
  */
 @RequestMapping("/contractList")
 public String contractList(
  @RequestParam(defaultValue = "1") int page,
  @RequestParam(defaultValue = "5")int size
 ) {
  //创建example
  ContractExample example = new ContractExample();
  //通过example创建criteria对象
  ContractExample.Criteria criteria = example.createCriteria();
  //向criteria对象设置条件
  criteria.andCompanyIdEqualTo(getLoginCompanyId());
  criteria.andStateEqualTo(1);
  example.setOrderByClause("create_time DESC");
  //发起调用
  PageInfo info = contractService.findAll(page, size, example);
  request.setAttribute("page",info);
  //跳转页面
  return "cargo/export/export-contractList";
 }

 /**
  * 分页查询报运单列表
  */
 @RequestMapping("/list")
 public String list(
  @RequestParam(defaultValue = "1") int page,
  @RequestParam(defaultValue = "10") int size
 ) {
  //创建example对象
  ExportExample example = new ExportExample();
  //创建criteria
  ExportExample.Criteria criteria = example.createCriteria();
  //向criteria设置条件
  criteria.andCompanyIdEqualTo(getLoginCompanyId());
  //调用方法查询
  PageInfo info = exportService.findAll(example, page, size);
  request.setAttribute("page", info);
  //跳转页面
  return "cargo/export/export-list";
 }

 /**
  * 进入到新增页面
  * /cargo/export/toExport.do
  */
 @RequestMapping("/toExport")
 public String toExport(String id) {
  request.setAttribute("id",id);
  return "cargo/export/export-toExport";
 }

 /**
  * 新增或修改
  * /cargo/export/edit.do
  */
 @RequestMapping("/edit")
 public String edit(Export export) {
  //设置企业参数
  export.setCompanyId(getLoginCompanyId());
  export.setCompanyName(getLoginCompanyName());
  //判断报运单是否存在id
  if (StringUtils.isEmpty(export.getId())) {
   //不存在id，调用service保存
   exportService.save(export);
  } else {
   //存在id，调用service更新
   exportService.update(export);
  }
  //重定向
  return "redirect:/cargo/export/list.do";
 }

 /**
  * 进入修改页面
  */
 @RequestMapping("/toUpdate")
 public String toUpdate(String id) {
  //根据id查询报运单
  Export export = exportService.findById(id);
  request.setAttribute("export",export);
  //根据报运单id查询商品
  ExportProductExample example = new ExportProductExample();
  ExportProductExample.Criteria criteria = example.createCriteria();
  criteria.andExportIdEqualTo(id);
  List<ExportProduct> list = exportProductService.findAll(example);
  request.setAttribute("eps",list);
  //跳转页面
  return "cargo/export/export-update";
 }

 /**
  * 海关报运方法
  * /cargo/export/exportE.do?id=d9355915-a337-456e-9fc1-7e92d7d69e27
  */
 @RequestMapping("/exportE")
 public String exportE(String id) {
  exportService.exportE(id);
  return "redirect:/cargo/export/list.do";
 }
}
